package omq.screen;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.security.Security;
import java.util.HashMap;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;

import omq.api.logger.OMQLogger;
import omq.constants.OMQConstants;
import omq.exception.OMQException;
import omq.panel.CheckConnPanel;
import omq.panel.DeleteMsgPanel;
import omq.panel.EditPreferencesPanel;
import omq.panel.EmptyQueuePanel;
import omq.panel.GetQueuePropertiesPanel;
import omq.panel.OmqPanel;
import omq.panel.RecieveMsgPanel;
import omq.panel.PeekMsgPanel;
import omq.panel.SendMsgPanel;

import component.InfoPanel;
import component.OmqClientMenu;

import constant.OmqTesterClientConst;


public class OmqTesterMainScreen extends DefaultScreen{
	/**
	 * 
	 */
	private static final long serialVersionUID = -4844377401471797053L;
	public static HashMap prefMap = null;
	OmqPanel actionsCards;
	protected EditPreferencesPanel editPrefPanel;
//	protected ClearPreferencesPanel clearPrefPanel;
	protected DeleteMsgPanel deleteMsgPanel;
	protected RecieveMsgPanel recieveMsgPanel;
//	protected OpenConnPanel openConnPanel;
//	protected CloseConnPanel closeConnPanel;
	protected CheckConnPanel checkConnPanel ;
	protected PeekMsgPanel peekMsgPanel;
	protected SendMsgPanel sendMsgPanel;
	
	protected EmptyQueuePanel emptyQueuePanel;
	protected GetQueuePropertiesPanel getQueuePropPanel;
	
	public OmqTesterMainScreen(){
		super();
		setName("Main");
	}
	public OmqTesterMainScreen(String title){
		super(title);
		setName("Main");
	}

	protected void setPanels(){
		
		editPrefPanel = new EditPreferencesPanel();
//		clearPrefPanel = new ClearPreferencesPanel();
		deleteMsgPanel = new DeleteMsgPanel();
		recieveMsgPanel = new RecieveMsgPanel();
//		openConnPanel = new OpenConnPanel();
//		closeConnPanel = new CloseConnPanel();
		checkConnPanel = new CheckConnPanel();
		peekMsgPanel = new PeekMsgPanel();
		sendMsgPanel = new SendMsgPanel();
		
		emptyQueuePanel = new EmptyQueuePanel();
		getQueuePropPanel = new GetQueuePropertiesPanel();
		
		lowerPanel.setLayout(new CardLayout());
		lowerPanel.add(new InfoPanel(),OmqTesterClientConst.WELCOME_PAGE);
		lowerPanel.add(sendMsgPanel,OmqTesterClientConst.ACTION_SEND_MSG);
		lowerPanel.add(editPrefPanel,OmqTesterClientConst.PREFERENCES_EDIT);
//		lowerPanel.add(clearPrefPanel,OmqTesterClientConst.PREFERENCES_CLEAR);
		lowerPanel.add(deleteMsgPanel,OmqTesterClientConst.ACTION_DELETE_MSG);
		lowerPanel.add(recieveMsgPanel,OmqTesterClientConst.ACTION_RECIEVE_MSG);
		lowerPanel.add(peekMsgPanel,OmqTesterClientConst.ACTION_PEEK_MSG);
//		lowerPanel.add(openConnPanel,OmqTesterClientConst.ACTION_OPEN_CONNECTION);
//		lowerPanel.add(closeConnPanel,OmqTesterClientConst.ACTION_CLOSE_CONNECTION);
		
		lowerPanel.add(emptyQueuePanel,OmqTesterClientConst.ACTION_EMPTY_QUEUE);
		lowerPanel.add(getQueuePropPanel,OmqTesterClientConst.ACTION_QUEUE_PROPERTIES);
		
		lowerPanel.add(checkConnPanel,OmqTesterClientConst.ACTION_CHECK_CONNECTION);
		lowerPanel.add(new JLabel("<html><body height='350' width='600' style='font-size:12px;text-align:center;'>"+OmqPanel.getTitle("Online MQ Client Application")+"<br>Version : " + OmqTesterClientConst.CLIENT_VERSION + "<br>Created : "+OmqTesterClientConst.CLIENT_VERSION_DATE+"<br> By : OMQ Team<br> Website : <a href='http://www.onlinemq.com' target='_blank'>http://www.onlinemq.com</a></body></html>"),OmqTesterClientConst.HELP_ABOUT);
		
		((CardLayout)lowerPanel.getLayout()).show(lowerPanel,OmqTesterClientConst.WELCOME_PAGE);
		
	}
	
	protected void addMenu() {
        OmqClientMenu.getInstance().addActionsMenu(this);
        OmqClientMenu.getInstance().addPrefMenu(this);
        OmqClientMenu.getInstance().addHelpMenu(this);
        setJMenuBar(OmqClientMenu.getInstance().getMenuBar());
	}
	
	public void actionPerformed(ActionEvent arg0) {
		OMQLogger.getInstance().debug("Action performed : Main screen");
		JMenuItem source = (JMenuItem)(arg0.getSource());
		String action = source.getActionCommand();
		
		OMQLogger.getInstance().debug("command : " + action);
		//The user requested to close the omq application
		if(action.equals(OmqTesterClientConst.ACTION_CLOSE_CLIENT)){
			OMQLogger.getInstance().debug("User request : exit Main screen");
			System.exit(0);
		}else{
			try {
				editPrefPanel.refreshPrefs();
//				clearPrefPanel.showOpenInfo();
				deleteMsgPanel.resetPanel();
				recieveMsgPanel.resetPanel();
				peekMsgPanel.resetPanel();
				sendMsgPanel.resetPanel();
//				openConnPanel.resetPanel();
//				closeConnPanel.resetPanel();
				checkConnPanel.resetPanel();
				emptyQueuePanel.resetPanel();
				getQueuePropPanel.resetPanel();
				refreshPreferencesObject();
				
				CardLayout cl = (CardLayout)(lowerPanel.getLayout());
				cl.show(lowerPanel, action);
			} catch (RuntimeException e) {
				throw new OMQException(e.getMessage());
			}
		}
	}
	
	/**
	 * Lauch the tester client - Main
	 * @param args
	 */
	public static void main(String[] args){
		try {
			OmqTesterMainScreen.clearPref(false);
		} catch (BackingStoreException e) {
			e.printStackTrace();
		}
		// probably important?
//		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
//		System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol");
//		System.setProperty("javax.net.ssl.keyStore","");
//		System.setProperty("javax.net.ssl.keyStorePassword","ofermi");
		
		// not sure what are these, it works without these:
//		System.setProperty("javax.net.ssl.trustStore",args[0]);
//		System.setProperty("javax.net.ssl.trustStorePassword",args[1]);

		// force debug message of the SSL
		//		System.setProperty("javax.net.debug","all");

        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
            	createAndShowGUI();
            }
        });
	}

	private static void createAndShowGUI(){
		DefaultScreen screen = new OmqTesterMainScreen("Online MQ tester client");
        screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         
        screen.setVisible(true);
	}


	public void refreshPreferencesObject(){
		//Setting the prefrences into the hashmap that we will send to any method the user chooses.
		prefMap = new HashMap();
		prefMap.put(OMQConstants.MAP_PARAM_USER,myPreferences.get(OmqTesterClientConst.PREF_USER_NAME,OmqTesterClientConst.PREF_USER_NAME));
		prefMap.put(OMQConstants.MAP_PARAM_PASSWORD,myPreferences.get(OmqTesterClientConst.PREF_PASSWORD,OmqTesterClientConst.PREF_PASSWORD)); 
		prefMap.put(OMQConstants.MAP_PARAM_QMGR_NAME,myPreferences.get(OmqTesterClientConst.PREF_QUEUE_MGR,OmqTesterClientConst.PREF_QUEUE_MGR));
		prefMap.put(OMQConstants.MAP_PARAM_QUEUE_NAME,myPreferences.get(OmqTesterClientConst.PREF_QUEUE_NAME,OmqTesterClientConst.PREF_QUEUE_NAME));
		
	}
	
	public static void clearPref(boolean toClear) throws BackingStoreException{
		if(toClear){
			Preferences myPreferences = Preferences.userNodeForPackage(OmqTesterMainScreen.class);
			myPreferences.clear();
		}
	}
}
